<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="600px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="友链地址" prop="path">
        <el-input
          clearable
          v-model.trim="form.path"
          size="mini"
          type="textarea"
          :rows="2"
          maxlength="255"
          show-word-limit
          placeholder="请输入友链地址"
        />
      </el-form-item>
      <el-form-item label="站点名称" prop="siteName">
        <el-input
          v-model="form.siteName"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入站点名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="友链邮箱" prop="email">
        <el-input
          v-model="form.email"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入友链邮箱"
        ></el-input>
      </el-form-item>
      <el-form-item label="友链描述" prop="desc">
        <el-input
          clearable
          v-model.trim="form.desc"
          size="mini"
          type="textarea"
          :rows="3"
          maxlength="255"
          show-word-limit
          placeholder="请输入友链描述"
        />
      </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkGetButton from "@/components/common/mk-get-button";
import MkBackList from "@/components/common/mk-back-list";
import {
  dirFriendsCreate,
  dirFriendsUpdate,
} from "@/api/modules/sys.friends.api";

export default {
  name: "get-friends-info",
  components: { MkBackList, MkGetButton },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: {
        path: "",
        siteName: "",
        email: "",
        desc: "",
      },
      isAdd: true,
      rules: {
        path: [{ required: true, message: "请输入友链地址", trigger: "blur" }],

        siteName: [
          { required: true, message: "请输入友链站点", trigger: "blur" },
        ],

        email: [
          { required: true, message: "请输入友链邮箱", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],

        desc: [{ required: true, message: "请输入友链描述", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let _FUC = "";
          _FUC = this.type == "create" ? dirFriendsCreate : dirFriendsUpdate;
          _FUC(this.form)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = {
        path: "",
        siteName: "",
        email: "",
        desc: "",
      };
    },
  },
};
</script>

<style lang="scss" scoped></style>
